package com.rzl.expense.quartz.util;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author zxx
 * @Description 加密处理
 */
public class MD5Util {

    public final static String encrypt(String s) {
        char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

        byte[] byteInput = s.getBytes();
        //获取MD5摘要算法的MessageDigest对象
        try {
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            //使用指定的字节更新摘要
            mdInst.update(byteInput);
            //获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];  //char占两个字节
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];  //右移四位，高四位清空 取低四位的值
            }
            return new String(str);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 测试
/*    public static void main(String[] args) throws Exception {
        String encrypt = encrypt(String.format("%s%s%s%s", "external_key", "external_id", "external_key", "2021-01-26 11:19:00"));
        System.out.println(encrypt);
    }*/
}


